package processors

import (
	"fmt"
	"go-demo/designpattern/templatemethod/ordercenter"
)

// 默认订单处理器
type DefaultOrderProcessor struct {
}

func (d *DefaultOrderProcessor) ValidateOrder(order *ordercenter.Order) error {
	fmt.Println("执行标准订单验证...")
	if len(order.Items) == 0 {
		return fmt.Errorf("订单 %s 没有商品", order.ID)
	}
	if order.UserID == "" {
		return fmt.Errorf("订单 %s 缺少用户ID", order.ID)
	}
	return nil
}

func (d *DefaultOrderProcessor) CalculatePrice(order *ordercenter.Order) {
	fmt.Println("计算订单总价...")
	total := 0.0
	for _, item := range order.Items {
		total += item.Price * float64(item.Quantity)
	}
	order.TotalAmount = total
	order.FinalAmount = total
}

func (d *DefaultOrderProcessor) ApplyDiscounts(order *ordercenter.Order) {
	// 默认不应用折扣
}

func (d *DefaultOrderProcessor) DeductInventory(order *ordercenter.Order) error {
	fmt.Println("扣减商品库存...")
	for _, item := range order.Items {
		fmt.Printf("  - 商品 %s: 扣减 %d 件库存\n", item.ProductID, item.Quantity)
	}
	return nil
}

func (d *DefaultOrderProcessor) GeneratePayment(order *ordercenter.Order) error {
	fmt.Printf("生成支付请求: ￥%.2f\n", order.FinalAmount)
	return nil
}

func (d *DefaultOrderProcessor) NotifyUser(order *ordercenter.Order) {
	fmt.Printf("发送订单通知给用户 %s\n", order.UserID)
}

func (d *DefaultOrderProcessor) LogOrder(order *ordercenter.Order) {
	fmt.Printf("记录订单日志: %+v\n", order)
}
